// Copyright 2022 a.o.smith ALL Rights Reserved
package com.liuxinlong.modules.butler.service.impl;

import com.liuxinlong.common.AosException;
import com.liuxinlong.common.SnowFlake;
import com.liuxinlong.constant.ThreadLocalConstants;
import com.liuxinlong.enums.ReturnCodes;
import com.liuxinlong.modules.butler.service.LecturerService;
import com.liuxinlong.modules.dao.LecturerDao;
import com.liuxinlong.modules.entity.Lecturer;
import com.liuxinlong.modules.entity.User;
import com.liuxinlong.modules.system.service.FileManageService;
import com.liuxinlong.utils.ObjectUtils;
import com.liuxinlong.utils.ThreadlocalUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 讲师信息业务接口层实现
 *
 * @author liuxinlong@hotwater.com.cn
 * @version 1.0.0
 * @since 2022-12-08
 */
@Service
@Slf4j
public class LecturerServiceImpl implements LecturerService {

    @Autowired
    private LecturerDao lecturerDao;

    @Autowired
    private FileManageService fileManageService;

    @Override
    public List<Map<String, Object>> pageLecturerInfo(Map<String, Object> queryParam) {
        int startNum = (int) queryParam.get("startNum");
        List<Lecturer> originalList = lecturerDao.pageLecturerInfo(queryParam);
        return completeData(originalList, startNum);
    }

    @Override
    public int getLecturerInfoCount(Map<String, Object> queryParam) {
        return lecturerDao.getLecturerInfoCount(queryParam);
    }

    @Transactional
    @Override
    public void addLecturerInfo(Lecturer lecturer) {
        User current = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        lecturer.setCreateUser(current.getId());
        String id = SnowFlake.nextIdStr();
        lecturer.setId(id);
        lecturerDao.insert(lecturer);
        fileManageService.updateBusinessFile(lecturer.getPicture(), id);
    }

    @Override
    @Transactional
    public void updateLecturer(Lecturer lecturer) {
        String id = lecturer.getId();
        Lecturer oldInfo = lecturerDao.selectById(id);
        if (ObjectUtils.isEmpty(oldInfo)) {
            throw new AosException(ReturnCodes.HTTP_PARAM_ERROR, "修改失败，原信息不存在！");
        }
        User current = (User) ThreadlocalUtils.getCache(ThreadLocalConstants.USER_INFO);
        lecturer.setUpdateUser(current.getId());
        lecturerDao.updateById(lecturer);
        fileManageService.updateBusinessFile(lecturer.getPicture(), id);
    }

    @Override
    public void deleteLecturer(String id) {
        lecturerDao.deleteById(id);
    }

    /**
     * 数据补全
     *
     * @param originalList 元数据
     * @param startNum     起始查询数
     * @return 补全后数据
     */
    private List<Map<String, Object>> completeData(List<Lecturer> originalList, int startNum) {
        if (ObjectUtils.isEmpty(originalList)) {
            return new ArrayList<>();
        }
        List<Map<String, Object>> resultList = new ArrayList<>(originalList.size());
        int sort = startNum + 1;
        for (Lecturer lecturer : originalList) {
            Map<String, Object> item = ObjectUtils.object2Map(lecturer);
            item.put("sort", sort);
            resultList.add(item);
            sort++;
        }
        return resultList;
    }
}
